<?php

$_POST['FraCalc'] = str_replace(',', '.', $_POST['FraCalc']);
$whereCause = "WHERE req_Tmp = {$_POST['TmpOp']} AND req_htr = {$_POST['htr_ID']} AND req_Amount = '{$_POST['FraCalc']}'
					AND sysUser = {$_SESSION[$SESSID]} AND sysOffice = {$_SESSION[$SESSCD]}
				   AND req_Cli = {$_POST['OBJCTCL']} AND req_Bnf = {$_POST['OBJCTRC']}";
if (isset($_POST['rtreq_cancel']) && $_POST['rtreq_cancel'] == 1) {
    $GLOBALS['MYT']->db_query("UPDATE hd_cambreqday SET Status = 3 {$whereCause}");
    $GLOBALS['MYT']->db_query("UPDATE hd_cambreq SET Status = 3 {$whereCause}");
    echo '<script charset="utf-8" type="text/javascript">document.InvoiceProperties.action=\'_response/call.php?get=Operative/remittancecalc\';
		subform(\'InvoiceProperties\',\'inv_Result\');</script>';
} else {
    $GET = $GLOBALS['MYT']->db_array("SELECT Status FROM hd_cambreqday {$whereCause} AND Status != 3");
    if (!isset($GET[1])) {
        $GETRateConfig = $GLOBALS['MYT']->db_array("SELECT ast_Value, htr_Points FROM systemoffice.administration_settings INNER JOIN officeschema.hd_cambiosdia ON htr_ID = {$_POST['htr_ID']} WHERE ast_ID = 1");
        $GETRMV = $GLOBALS['MYT']->db_array("SELECT sysTime, Status FROM hd_cambreqday {$whereCause} AND Status = 3");
        if (!isset($GETRMV[1])) {
            if ($GETRateConfig[1]['ast_Value'] == 2 && $GETRateConfig[1]['htr_Points'] >= $_POST['PTMod'] && $_POST['PTMod'] > 0) {
                $GLOBALS['MYT']->db_query("INSERT INTO hd_cambreq (sysUser, sysTime, req_Pts, req_htr, req_Cli, req_Bnf, req_Tmp, req_Rate, sysOffice, req_Amount,Status,apr_Rate,apr_Pts,aprTime,aprUser) VALUES ({$_SESSION[$SESSID]},'{$sysTime}','{$_POST['PTMod']}','{$_POST['htr_ID']}','{$_POST['OBJCTCL']}','{$_POST['OBJCTRC']}','{$_POST['TmpOp']}','{$_POST['RTMod']}',{$_SESSION[$SESSCD]},'{$_POST['FraCalc']}',2,'{$_POST['RTMod']}','{$_POST['PTMod']}','{$sysTime}',0)");
                $GLOBALS['MYT']->db_query("INSERT INTO hd_cambreqday (sysUser, sysTime, req_Pts, req_htr, req_Cli, req_Bnf, req_Tmp, req_Rate, sysOffice, req_Amount,Status,apr_Rate,apr_Pts,aprTime,aprUser) VALUES ({$_SESSION[$SESSID]},'{$sysTime}','{$_POST['PTMod']}','{$_POST['htr_ID']}','{$_POST['OBJCTCL']}','{$_POST['OBJCTRC']}','{$_POST['TmpOp']}','{$_POST['RTMod']}',{$_SESSION[$SESSCD]},'{$_POST['FraCalc']}',2,'{$_POST['RTMod']}','{$_POST['PTMod']}','{$sysTime}',0)");
                echo '<script charset="utf-8" type="text/javascript">document.InvoiceProperties.action=\'_response/call.php?get=Operative/remittancecalc\';
			subform(\'InvoiceProperties\',\'inv_Result\');</script>';
            } else {
                $GLOBALS['MYT']->db_query("INSERT INTO hd_cambreq (sysUser, sysTime, req_Pts, req_htr, req_Cli, req_Bnf, req_Tmp, req_Rate, sysOffice, req_Amount) VALUES ({$_SESSION[$SESSID]},'{$sysTime}','{$_POST['PTMod']}','{$_POST['htr_ID']}','{$_POST['OBJCTCL']}','{$_POST['OBJCTRC']}','{$_POST['TmpOp']}','{$_POST['RTMod']}',{$_SESSION[$SESSCD]},'{$_POST['FraCalc']}')");
                $GLOBALS['MYT']->db_query("INSERT INTO hd_cambreqday (sysUser, sysTime, req_Pts, req_htr, req_Cli, req_Bnf, req_Tmp, req_Rate, sysOffice, req_Amount) VALUES ({$_SESSION[$SESSID]},'{$sysTime}','{$_POST['PTMod']}','{$_POST['htr_ID']}','{$_POST['OBJCTCL']}','{$_POST['OBJCTRC']}','{$_POST['TmpOp']}','{$_POST['RTMod']}',{$_SESSION[$SESSCD]},'{$_POST['FraCalc']}')");
            }
        } else {
            if ($GETRateConfig[1]['ast_Value'] == 2 && $GETRateConfig[1]['htr_Points'] >= $_POST['PTMod'] && $_POST['PTMod'] > 0) {
                $GLOBALS['MYT']->db_query("UPDATE hd_cambreq SET sysTime = '{$sysTime}', req_Pts = '{$_POST['PTMod']}', req_htr = '{$_POST['htr_ID']}', req_Rate = '{$_POST['RTMod']}', Status = 2, apr_Rate = '{$_POST['RTMod']}',apr_Pts = '{$_POST['PTMod']}',aprTime = '{$sysTime}',aprUser = 0
									WHERE sysTime = '{$GETRMV[1]['sysTime']}' AND sysUser = {$_SESSION[$SESSID]} AND req_Cli = {$_POST['OBJCTCL']} AND req_Bnf = {$_POST['OBJCTRC']} AND req_Amount = '{$_POST['FraCalc']}' AND req_Tmp = {$_POST['TmpOp']} AND sysOffice = {$_SESSION[$SESSCD]}");
                $GLOBALS['MYT']->db_query("UPDATE hd_cambreqday SET sysTime = '{$sysTime}', req_Pts = '{$_POST['PTMod']}', req_htr = '{$_POST['htr_ID']}', req_Rate = '{$_POST['RTMod']}', Status = 2, apr_Rate = '{$_POST['RTMod']}',apr_Pts = '{$_POST['PTMod']}',aprTime = '{$sysTime}',aprUser = 0
									WHERE sysTime = '{$GETRMV[1]['sysTime']}' AND sysUser = {$_SESSION[$SESSID]} AND req_Cli = {$_POST['OBJCTCL']} AND req_Bnf = {$_POST['OBJCTRC']} AND req_Amount = '{$_POST['FraCalc']}' AND req_Tmp = {$_POST['TmpOp']} AND sysOffice = {$_SESSION[$SESSCD]}");
                echo '<script charset="utf-8" type="text/javascript">document.InvoiceProperties.action=\'_response/call.php?get=Operative/remittancecalc\';
			subform(\'InvoiceProperties\',\'inv_Result\');</script>';
            } else {

                $GLOBALS['MYT']->db_query("UPDATE hd_cambreq SET sysTime = '{$sysTime}', req_Pts = '{$_POST['PTMod']}', req_htr = '{$_POST['htr_ID']}', req_Rate = '{$_POST['RTMod']}', Status = 1, apr_Rate = null,apr_Pts = null,aprTime = null,aprUser = null
									WHERE sysTime = '{$GETRMV[1]['sysTime']}' AND sysUser = {$_SESSION[$SESSID]} AND req_Cli = {$_POST['OBJCTCL']} AND req_Bnf = {$_POST['OBJCTRC']} AND req_Amount = '{$_POST['FraCalc']}' AND req_Tmp = {$_POST['TmpOp']} AND sysOffice = {$_SESSION[$SESSCD]}");
                $GLOBALS['MYT']->db_query("UPDATE hd_cambreqday SET sysTime = '{$sysTime}', req_Pts = '{$_POST['PTMod']}', req_htr = '{$_POST['htr_ID']}', req_Rate = '{$_POST['RTMod']}', Status = 1, apr_Rate = null,apr_Pts = null,aprTime = null,aprUser = null
									WHERE sysTime = '{$GETRMV[1]['sysTime']}' AND sysUser = {$_SESSION[$SESSID]} AND req_Cli = {$_POST['OBJCTCL']} AND req_Bnf = {$_POST['OBJCTRC']} AND req_Amount = '{$_POST['FraCalc']}' AND req_Tmp = {$_POST['TmpOp']} AND sysOffice = {$_SESSION[$SESSCD]}");
            }
        }
        $GET = $GLOBALS['MYT']->db_array("SELECT Status FROM hd_cambreqday {$whereCause} AND Status != 3");
    }
}
if (isset($GET[1])) {
    mn_field('hidden', 'ED', 'rtreq_cancel', '', '', '', '', '');
    if ($GET[1]['Status'] == 1) {
        echo 'A Aguardar | ';
        mn_field('button', 'ED', 'ref_refresh', 'Verificar', '', '', '', '"
			onclick="document.InvoiceProperties.action=\'_response/call.php?get=Operative/remittancecalc\';
			subform(\'InvoiceProperties\',\'inv_Result\');');
        mn_field('button', 'ED', 'ref_cancel', 'Cancelar', '', '', '', '"
			onclick="document.InvoiceProperties.action=\'_response/call.php?get=Operative/opv_remittancerate\';
			document.InvoiceProperties.rtreq_cancel.value=1;
			subform(\'InvoiceProperties\',\'ORRate\');');
    } elseif ($GET[1]['Status'] == 2) {
        echo '<div class="Confirm">Aceite</div>';
    } elseif ($GET[1]['Status'] == 5) {
        echo '<div class="Error">Rejeitado</div>';
    }
}